

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>federatedml.statistic.statics &mdash; FATE 1.0 documentation</title>
  

  
  
  
  

  
  <script type="text/javascript" src="../../../_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
        <script type="text/javascript" src="../../../_static/jquery.js"></script>
        <script type="text/javascript" src="../../../_static/underscore.js"></script>
        <script type="text/javascript" src="../../../_static/doctools.js"></script>
        <script type="text/javascript" src="../../../_static/language_data.js"></script>
    
    <script type="text/javascript" src="../../../_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../../index.html" class="icon icon-home"> FATE
          

          
          </a>

          
            
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <!-- Local TOC -->
              <div class="local-toc"></div>
            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../index.html">FATE</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../../index.html">Module code</a> &raquo;</li>
        
      <li>federatedml.statistic.statics</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for federatedml.statistic.statics</h1><div class="highlight"><pre>
<span></span><span class="c1">#</span>
<span class="c1">#  Copyright 2019 The FATE Authors. All Rights Reserved.</span>
<span class="c1">#</span>
<span class="c1">#  Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);</span>
<span class="c1">#  you may not use this file except in compliance with the License.</span>
<span class="c1">#  You may obtain a copy of the License at</span>
<span class="c1">#</span>
<span class="c1">#      http://www.apache.org/licenses/LICENSE-2.0</span>
<span class="c1">#</span>
<span class="c1">#  Unless required by applicable law or agreed to in writing, software</span>
<span class="c1">#  distributed under the License is distributed on an &quot;AS IS&quot; BASIS,</span>
<span class="c1">#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span>
<span class="c1">#  See the License for the specific language governing permissions and</span>
<span class="c1">#  limitations under the License.</span>
<span class="c1">#</span>

<span class="kn">import</span> <span class="nn">functools</span>
<span class="kn">import</span> <span class="nn">math</span>
<span class="kn">import</span> <span class="nn">sys</span>

<span class="kn">from</span> <span class="nn">arch.api.utils</span> <span class="k">import</span> <span class="n">log_utils</span>
<span class="kn">from</span> <span class="nn">federatedml.feature.binning.quantile_binning</span> <span class="k">import</span> <span class="n">QuantileBinning</span>
<span class="kn">from</span> <span class="nn">federatedml.feature.quantile_summaries</span> <span class="k">import</span> <span class="n">QuantileSummaries</span>
<span class="kn">from</span> <span class="nn">federatedml.feature.instance</span> <span class="k">import</span> <span class="n">Instance</span>
<span class="kn">from</span> <span class="nn">federatedml.param.param</span> <span class="k">import</span> <span class="n">FeatureBinningParam</span>
<span class="kn">from</span> <span class="nn">federatedml.statistic</span> <span class="k">import</span> <span class="n">data_overview</span>

<span class="n">LOGGER</span> <span class="o">=</span> <span class="n">log_utils</span><span class="o">.</span><span class="n">getLogger</span><span class="p">()</span>


<div class="viewcode-block" id="SummaryStatistics"><a class="viewcode-back" href="../../../federatedml.statistic.html#federatedml.statistic.statics.SummaryStatistics">[docs]</a><span class="k">class</span> <span class="nc">SummaryStatistics</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">abnormal_list</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">abnormal_list</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">abnormal_list</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">abnormal_list</span> <span class="o">=</span> <span class="n">abnormal_list</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">sum</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">sum_square</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">max_value</span> <span class="o">=</span> <span class="o">-</span> <span class="n">sys</span><span class="o">.</span><span class="n">maxsize</span> <span class="o">-</span> <span class="mi">1</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">min_value</span> <span class="o">=</span> <span class="n">sys</span><span class="o">.</span><span class="n">maxsize</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">count</span> <span class="o">=</span> <span class="mi">0</span>

<div class="viewcode-block" id="SummaryStatistics.add_value"><a class="viewcode-back" href="../../../federatedml.statistic.html#federatedml.statistic.statics.SummaryStatistics.add_value">[docs]</a>    <span class="k">def</span> <span class="nf">add_value</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">value</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">abnormal_list</span><span class="p">:</span>
            <span class="k">return</span>

        <span class="k">try</span><span class="p">:</span>
            <span class="n">value</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">TypeError</span><span class="p">:</span>
            <span class="n">LOGGER</span><span class="o">.</span><span class="n">warning</span><span class="p">(</span><span class="s1">&#39;The value </span><span class="si">{}</span><span class="s1"> cannot be converted to float&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">value</span><span class="p">))</span>
            <span class="k">return</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">count</span> <span class="o">+=</span> <span class="mi">1</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">sum</span> <span class="o">+=</span> <span class="n">value</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">sum_square</span> <span class="o">+=</span> <span class="n">value</span> <span class="o">**</span> <span class="mi">2</span>
        <span class="k">if</span> <span class="n">value</span> <span class="o">&gt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">max_value</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">max_value</span> <span class="o">=</span> <span class="n">value</span>
        <span class="k">if</span> <span class="n">value</span> <span class="o">&lt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">min_value</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">min_value</span> <span class="o">=</span> <span class="n">value</span></div>

<div class="viewcode-block" id="SummaryStatistics.merge"><a class="viewcode-back" href="../../../federatedml.statistic.html#federatedml.statistic.statics.SummaryStatistics.merge">[docs]</a>    <span class="k">def</span> <span class="nf">merge</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">count</span> <span class="o">+=</span> <span class="n">other</span><span class="o">.</span><span class="n">count</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">sum</span> <span class="o">+=</span> <span class="n">other</span><span class="o">.</span><span class="n">sum</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">sum_square</span> <span class="o">+=</span> <span class="n">other</span><span class="o">.</span><span class="n">sum_square</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">max_value</span> <span class="o">&lt;</span> <span class="n">other</span><span class="o">.</span><span class="n">max_value</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">max_value</span> <span class="o">=</span> <span class="n">other</span><span class="o">.</span><span class="n">max_value</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">min_value</span> <span class="o">&gt;</span> <span class="n">other</span><span class="o">.</span><span class="n">min_value</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">min_value</span> <span class="o">=</span> <span class="n">other</span><span class="o">.</span><span class="n">min_value</span></div>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">mean</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">sum</span> <span class="o">/</span> <span class="bp">self</span><span class="o">.</span><span class="n">count</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">variance</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">mean</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">mean</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">sum_square</span> <span class="o">/</span> <span class="bp">self</span><span class="o">.</span><span class="n">count</span> <span class="o">-</span> <span class="mi">2</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">sum</span> <span class="o">*</span> <span class="n">mean</span> <span class="o">/</span> <span class="bp">self</span><span class="o">.</span><span class="n">count</span> <span class="o">+</span> <span class="n">mean</span> <span class="o">**</span> <span class="mi">2</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">std_variance</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">math</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">variance</span><span class="p">)</span></div>


<div class="viewcode-block" id="MultivariateStatisticalSummary"><a class="viewcode-back" href="../../../federatedml.statistic.html#federatedml.statistic.statics.MultivariateStatisticalSummary">[docs]</a><span class="k">class</span> <span class="nc">MultivariateStatisticalSummary</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>

<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data_instances</span><span class="p">,</span> <span class="n">cols_index</span><span class="o">=-</span><span class="mi">1</span><span class="p">,</span> <span class="n">abnormal_list</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">finish_fit_statics</span> <span class="o">=</span> <span class="kc">False</span>     <span class="c1"># Use for static data</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">finish_fit_summaries</span> <span class="o">=</span> <span class="kc">False</span>   <span class="c1"># Use for quantile data</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">summary_statistics</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">quantile_summary_dict</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">cols_dict</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">medians</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">data_instances</span> <span class="o">=</span> <span class="n">data_instances</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">cols_index</span> <span class="o">=</span> <span class="n">cols_index</span>
        <span class="k">if</span> <span class="n">abnormal_list</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">abnormal_list</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">abnormal_list</span> <span class="o">=</span> <span class="n">abnormal_list</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_init_cols</span><span class="p">(</span><span class="n">data_instances</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_init_cols</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data_instances</span><span class="p">):</span>

        <span class="c1"># Already initialized</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">cols_dict</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">return</span>

        <span class="n">header</span> <span class="o">=</span> <span class="n">data_overview</span><span class="o">.</span><span class="n">get_header</span><span class="p">(</span><span class="n">data_instances</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">header</span> <span class="o">=</span> <span class="n">header</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">cols_index</span> <span class="o">==</span> <span class="o">-</span><span class="mi">1</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">cols</span> <span class="o">=</span> <span class="n">header</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">cols_index</span> <span class="o">=</span> <span class="p">[</span><span class="n">i</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">header</span><span class="p">))]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">cols</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="k">for</span> <span class="n">idx</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">cols_index</span><span class="p">:</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="n">idx</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">idx</span><span class="p">)</span>
                <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;In binning module, selected index: </span><span class="si">{}</span><span class="s2"> is not integer&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">idx</span><span class="p">))</span>

                <span class="k">if</span> <span class="n">idx</span> <span class="o">&gt;=</span> <span class="nb">len</span><span class="p">(</span><span class="n">header</span><span class="p">):</span>
                    <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                        <span class="s2">&quot;In binning module, selected index: </span><span class="si">{}</span><span class="s2"> exceed length of data dimension&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">idx</span><span class="p">))</span>
                <span class="n">cols</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">header</span><span class="p">[</span><span class="n">idx</span><span class="p">])</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">cols</span> <span class="o">=</span> <span class="n">cols</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">cols_dict</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="k">for</span> <span class="n">col</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">cols</span><span class="p">:</span>
            <span class="n">col_index</span> <span class="o">=</span> <span class="n">header</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">col</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">cols_dict</span><span class="p">[</span><span class="n">col</span><span class="p">]</span> <span class="o">=</span> <span class="n">col_index</span>

    <span class="k">def</span> <span class="nf">_static_sums</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Statics sum, sum_square, max_value, min_value,</span>
<span class="sd">        so that variance is available.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">partition_cal</span> <span class="o">=</span> <span class="n">functools</span><span class="o">.</span><span class="n">partial</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">static_in_partition</span><span class="p">,</span>
                                          <span class="n">cols_dict</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">cols_dict</span><span class="p">,</span>
                                          <span class="n">abnormal_list</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">abnormal_list</span><span class="p">)</span>
        <span class="n">summary_statistic_dict</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">data_instances</span><span class="o">.</span><span class="n">mapPartitions</span><span class="p">(</span><span class="n">partition_cal</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">summary_statistics</span> <span class="o">=</span> <span class="n">summary_statistic_dict</span><span class="o">.</span><span class="n">reduce</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">aggregate_statics</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">finish_fit_statics</span> <span class="o">=</span> <span class="kc">True</span>

    <span class="k">def</span> <span class="nf">_static_quantile_summaries</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Static summaries so that can query a specific quantile point</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">finish_fit_summaries</span> <span class="ow">is</span> <span class="kc">True</span><span class="p">:</span>
            <span class="k">return</span>

        <span class="n">partition_cal</span> <span class="o">=</span> <span class="n">functools</span><span class="o">.</span><span class="n">partial</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">static_summaries_in_partition</span><span class="p">,</span>
                                          <span class="n">cols_dict</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">cols_dict</span><span class="p">,</span>
                                          <span class="n">abnormal_list</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">abnormal_list</span><span class="p">)</span>
        <span class="n">quantile_summary_dict</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">data_instances</span><span class="o">.</span><span class="n">mapPartitions</span><span class="p">(</span><span class="n">partition_cal</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">quantile_summary_dict</span> <span class="o">=</span> <span class="n">quantile_summary_dict</span><span class="o">.</span><span class="n">reduce</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">aggregate_statics</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">finish_fit_summaries</span> <span class="o">=</span> <span class="kc">True</span>

<div class="viewcode-block" id="MultivariateStatisticalSummary.static_in_partition"><a class="viewcode-back" href="../../../federatedml.statistic.html#federatedml.statistic.statics.MultivariateStatisticalSummary.static_in_partition">[docs]</a>    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">static_in_partition</span><span class="p">(</span><span class="n">data_instances</span><span class="p">,</span> <span class="n">cols_dict</span><span class="p">,</span> <span class="n">abnormal_list</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Statics sums, sum_square, max and min value through one traversal</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        data_instances : DTable</span>
<span class="sd">            The input data</span>

<span class="sd">        cols_dict : dict</span>
<span class="sd">            Specify which column(s) need to apply statistic.</span>

<span class="sd">        abnormal_list: list</span>
<span class="sd">            Specify which values are not permitted.</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        Dict of SummaryStatistics object</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">summary_statistic_dict</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="k">for</span> <span class="n">col_name</span> <span class="ow">in</span> <span class="n">cols_dict</span><span class="p">:</span>
            <span class="n">summary_statistic_dict</span><span class="p">[</span><span class="n">col_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">SummaryStatistics</span><span class="p">(</span><span class="n">abnormal_list</span><span class="p">)</span>

        <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">instances</span> <span class="ow">in</span> <span class="n">data_instances</span><span class="p">:</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">instances</span><span class="p">,</span> <span class="n">Instance</span><span class="p">):</span>
                <span class="n">features</span> <span class="o">=</span> <span class="n">instances</span><span class="o">.</span><span class="n">features</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">features</span> <span class="o">=</span> <span class="n">instances</span>

            <span class="k">for</span> <span class="n">col_name</span><span class="p">,</span> <span class="n">col_index</span> <span class="ow">in</span> <span class="n">cols_dict</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
                <span class="n">value</span> <span class="o">=</span> <span class="n">features</span><span class="p">[</span><span class="n">col_index</span><span class="p">]</span>
                <span class="n">stat_obj</span> <span class="o">=</span> <span class="n">summary_statistic_dict</span><span class="p">[</span><span class="n">col_name</span><span class="p">]</span>
                <span class="n">stat_obj</span><span class="o">.</span><span class="n">add_value</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">summary_statistic_dict</span></div>

<div class="viewcode-block" id="MultivariateStatisticalSummary.static_summaries_in_partition"><a class="viewcode-back" href="../../../federatedml.statistic.html#federatedml.statistic.statics.MultivariateStatisticalSummary.static_summaries_in_partition">[docs]</a>    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">static_summaries_in_partition</span><span class="p">(</span><span class="n">data_instances</span><span class="p">,</span> <span class="n">cols_dict</span><span class="p">,</span> <span class="n">abnormal_list</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Statics sums, sum_square, max and min value through one traversal</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        data_instances : DTable</span>
<span class="sd">            The input data</span>

<span class="sd">        cols_dict : dict</span>
<span class="sd">            Specify which column(s) need to apply statistic.</span>

<span class="sd">        abnormal_list: list</span>
<span class="sd">            Specify which values are not permitted.</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        Dict of SummaryStatistics object</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">summary_dict</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="k">for</span> <span class="n">col_name</span> <span class="ow">in</span> <span class="n">cols_dict</span><span class="p">:</span>
            <span class="n">summary_dict</span><span class="p">[</span><span class="n">col_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">QuantileSummaries</span><span class="p">(</span><span class="n">abnormal_list</span><span class="o">=</span><span class="n">abnormal_list</span><span class="p">)</span>

        <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">instances</span> <span class="ow">in</span> <span class="n">data_instances</span><span class="p">:</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">instances</span><span class="p">,</span> <span class="n">Instance</span><span class="p">):</span>
                <span class="n">features</span> <span class="o">=</span> <span class="n">instances</span><span class="o">.</span><span class="n">features</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">features</span> <span class="o">=</span> <span class="n">instances</span>

            <span class="k">for</span> <span class="n">col_name</span><span class="p">,</span> <span class="n">col_index</span> <span class="ow">in</span> <span class="n">cols_dict</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
                <span class="n">value</span> <span class="o">=</span> <span class="n">features</span><span class="p">[</span><span class="n">col_index</span><span class="p">]</span>
                <span class="n">summary_obj</span> <span class="o">=</span> <span class="n">summary_dict</span><span class="p">[</span><span class="n">col_name</span><span class="p">]</span>
                <span class="n">summary_obj</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">summary_dict</span></div>

<div class="viewcode-block" id="MultivariateStatisticalSummary.aggregate_statics"><a class="viewcode-back" href="../../../federatedml.statistic.html#federatedml.statistic.statics.MultivariateStatisticalSummary.aggregate_statics">[docs]</a>    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">aggregate_statics</span><span class="p">(</span><span class="n">s_dict1</span><span class="p">,</span> <span class="n">s_dict2</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">s_dict1</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">s_dict2</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">None</span>
        <span class="k">if</span> <span class="n">s_dict1</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">s_dict2</span>
        <span class="k">if</span> <span class="n">s_dict2</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">s_dict1</span>

        <span class="n">new_dict</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="k">for</span> <span class="n">col_name</span><span class="p">,</span> <span class="n">static_1</span> <span class="ow">in</span> <span class="n">s_dict1</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="n">static_1</span><span class="o">.</span><span class="n">merge</span><span class="p">(</span><span class="n">s_dict2</span><span class="p">[</span><span class="n">col_name</span><span class="p">])</span>
            <span class="n">new_dict</span><span class="p">[</span><span class="n">col_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">static_1</span>
        <span class="k">return</span> <span class="n">new_dict</span></div>

<div class="viewcode-block" id="MultivariateStatisticalSummary.get_mean"><a class="viewcode-back" href="../../../federatedml.statistic.html#federatedml.statistic.statics.MultivariateStatisticalSummary.get_mean">[docs]</a>    <span class="k">def</span> <span class="nf">get_mean</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">cols_dict</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return the mean value(s) of the given column</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        cols_dict : dict</span>
<span class="sd">            Specify which column(s) need to apply statistic.</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        return a dict of result mean.</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_prepare_data</span><span class="p">(</span><span class="n">cols_dict</span><span class="p">,</span> <span class="s2">&quot;mean&quot;</span><span class="p">)</span></div>

<div class="viewcode-block" id="MultivariateStatisticalSummary.get_median"><a class="viewcode-back" href="../../../federatedml.statistic.html#federatedml.statistic.statics.MultivariateStatisticalSummary.get_median">[docs]</a>    <span class="k">def</span> <span class="nf">get_median</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">cols_dict</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="n">medians</span> <span class="o">=</span> <span class="p">{}</span>

        <span class="k">if</span> <span class="n">cols_dict</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">cols_dict</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">cols_dict</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">medians</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">medians</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_quantile_median</span><span class="p">()</span>

        <span class="k">for</span> <span class="n">col_name</span> <span class="ow">in</span> <span class="n">cols_dict</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">col_name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">medians</span><span class="p">:</span>
                <span class="n">LOGGER</span><span class="o">.</span><span class="n">warning</span><span class="p">(</span><span class="s2">&quot;The column </span><span class="si">{}</span><span class="s2">, has not set in selection parameters.&quot;</span>
                               <span class="s2">&quot;median values is not available&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">col_name</span><span class="p">))</span>
                <span class="k">continue</span>
            <span class="n">medians</span><span class="p">[</span><span class="n">col_name</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">medians</span><span class="p">[</span><span class="n">col_name</span><span class="p">]</span>

        <span class="k">return</span> <span class="n">medians</span></div>

<div class="viewcode-block" id="MultivariateStatisticalSummary.get_quantile_point"><a class="viewcode-back" href="../../../federatedml.statistic.html#federatedml.statistic.statics.MultivariateStatisticalSummary.get_quantile_point">[docs]</a>    <span class="k">def</span> <span class="nf">get_quantile_point</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">quantile</span><span class="p">,</span> <span class="n">cols_dict</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return the specific quantile point value</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        quantile : float, 0 &lt;= quantile &lt;= 1</span>
<span class="sd">            Specify which column(s) need to apply statistic.</span>

<span class="sd">        cols_dict : dict</span>
<span class="sd">            Specify which column(s) need to apply statistic.</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        return a dict of result quantile points.</span>
<span class="sd">        eg.</span>
<span class="sd">        quantile_point = {&quot;x1&quot;: 3, &quot;x2&quot;: 5... }</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">quantile_points</span> <span class="o">=</span> <span class="p">{}</span>

        <span class="k">if</span> <span class="n">cols_dict</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">cols_dict</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">cols_dict</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_static_quantile_summaries</span><span class="p">()</span>

        <span class="k">for</span> <span class="n">col_name</span> <span class="ow">in</span> <span class="n">cols_dict</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">col_name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">quantile_summary_dict</span><span class="p">:</span>
                <span class="n">LOGGER</span><span class="o">.</span><span class="n">warning</span><span class="p">(</span><span class="s2">&quot;The column </span><span class="si">{}</span><span class="s2">, has not set in selection parameters.&quot;</span>
                               <span class="s2">&quot;Quantile point query is not available&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">col_name</span><span class="p">))</span>
                <span class="k">continue</span>
            <span class="n">summary_obj</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">quantile_summary_dict</span><span class="p">[</span><span class="n">col_name</span><span class="p">]</span>
            <span class="n">quantile_point</span> <span class="o">=</span> <span class="n">summary_obj</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="n">quantile</span><span class="p">)</span>
            <span class="n">quantile_points</span><span class="p">[</span><span class="n">col_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">quantile_point</span>
        <span class="k">return</span> <span class="n">quantile_points</span></div>

    <span class="k">def</span> <span class="nf">_get_quantile_median</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">cols_index</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_cols_index</span><span class="p">()</span>
        <span class="n">bin_param</span> <span class="o">=</span> <span class="n">FeatureBinningParam</span><span class="p">(</span><span class="n">bin_num</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">cols</span><span class="o">=</span><span class="n">cols_index</span><span class="p">)</span>
        <span class="n">binning_obj</span> <span class="o">=</span> <span class="n">QuantileBinning</span><span class="p">(</span><span class="n">bin_param</span><span class="p">,</span> <span class="n">abnormal_list</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">abnormal_list</span><span class="p">)</span>
        <span class="n">split_points</span> <span class="o">=</span> <span class="n">binning_obj</span><span class="o">.</span><span class="n">fit_split_points</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">data_instances</span><span class="p">)</span>
        <span class="n">medians</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="k">for</span> <span class="n">col_name</span><span class="p">,</span> <span class="n">split_point</span> <span class="ow">in</span> <span class="n">split_points</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="n">medians</span><span class="p">[</span><span class="n">col_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">split_point</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
        <span class="k">return</span> <span class="n">medians</span>

    <span class="k">def</span> <span class="nf">_get_cols_index</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">cols_index</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">col</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">cols</span><span class="p">:</span>
            <span class="n">idx</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">cols_dict</span><span class="p">[</span><span class="n">col</span><span class="p">]</span>
            <span class="n">cols_index</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">idx</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">cols_index</span>

<div class="viewcode-block" id="MultivariateStatisticalSummary.get_variance"><a class="viewcode-back" href="../../../federatedml.statistic.html#federatedml.statistic.statics.MultivariateStatisticalSummary.get_variance">[docs]</a>    <span class="k">def</span> <span class="nf">get_variance</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">cols_dict</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_prepare_data</span><span class="p">(</span><span class="n">cols_dict</span><span class="p">,</span> <span class="s2">&quot;variance&quot;</span><span class="p">)</span></div>

<div class="viewcode-block" id="MultivariateStatisticalSummary.get_std_variance"><a class="viewcode-back" href="../../../federatedml.statistic.html#federatedml.statistic.statics.MultivariateStatisticalSummary.get_std_variance">[docs]</a>    <span class="k">def</span> <span class="nf">get_std_variance</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">cols_dict</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_prepare_data</span><span class="p">(</span><span class="n">cols_dict</span><span class="p">,</span> <span class="s2">&quot;std_variance&quot;</span><span class="p">)</span></div>

<div class="viewcode-block" id="MultivariateStatisticalSummary.get_max"><a class="viewcode-back" href="../../../federatedml.statistic.html#federatedml.statistic.statics.MultivariateStatisticalSummary.get_max">[docs]</a>    <span class="k">def</span> <span class="nf">get_max</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">cols_dict</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_prepare_data</span><span class="p">(</span><span class="n">cols_dict</span><span class="p">,</span> <span class="s2">&quot;max_value&quot;</span><span class="p">)</span></div>

<div class="viewcode-block" id="MultivariateStatisticalSummary.get_min"><a class="viewcode-back" href="../../../federatedml.statistic.html#federatedml.statistic.statics.MultivariateStatisticalSummary.get_min">[docs]</a>    <span class="k">def</span> <span class="nf">get_min</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">cols_dict</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_prepare_data</span><span class="p">(</span><span class="n">cols_dict</span><span class="p">,</span> <span class="s2">&quot;min_value&quot;</span><span class="p">)</span></div>

    <span class="k">def</span> <span class="nf">_prepare_data</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">cols_dict</span><span class="p">,</span> <span class="n">data_type</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return the specific static value(s) of the given column</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        cols_dict : dict</span>
<span class="sd">            Specify which column(s) need to apply statistic.</span>

<span class="sd">        data_type : str, &quot;mean&quot;, &quot;variance&quot;, &quot;std_variance&quot;, &quot;max_value&quot; or &quot;mim_value&quot;</span>
<span class="sd">            Specify which type to show.</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        return a list of result result. The order is the same as cols.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">finish_fit_statics</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_static_sums</span><span class="p">()</span>

        <span class="k">if</span> <span class="n">cols_dict</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">cols_dict</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">cols_dict</span>

        <span class="n">result</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="k">for</span> <span class="n">col_name</span><span class="p">,</span> <span class="n">col_index</span> <span class="ow">in</span> <span class="n">cols_dict</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="k">if</span> <span class="n">col_name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">cols_dict</span><span class="p">:</span>
                <span class="n">LOGGER</span><span class="o">.</span><span class="n">warning</span><span class="p">(</span><span class="s2">&quot;feature </span><span class="si">{}</span><span class="s2"> has not been static yet. Has been skipped&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">col_name</span><span class="p">))</span>
                <span class="k">continue</span>

            <span class="n">summary_obj</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">summary_statistics</span><span class="p">[</span><span class="n">col_name</span><span class="p">]</span>
            <span class="k">if</span> <span class="n">data_type</span> <span class="o">==</span> <span class="s1">&#39;mean&#39;</span><span class="p">:</span>
                <span class="n">result</span><span class="p">[</span><span class="n">col_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">summary_obj</span><span class="o">.</span><span class="n">mean</span>
            <span class="k">elif</span> <span class="n">data_type</span> <span class="o">==</span> <span class="s1">&#39;variance&#39;</span><span class="p">:</span>
                <span class="n">result</span><span class="p">[</span><span class="n">col_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">summary_obj</span><span class="o">.</span><span class="n">variance</span>
            <span class="k">elif</span> <span class="n">data_type</span> <span class="o">==</span> <span class="s1">&#39;max_value&#39;</span><span class="p">:</span>
                <span class="n">result</span><span class="p">[</span><span class="n">col_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">summary_obj</span><span class="o">.</span><span class="n">max_value</span>
            <span class="k">elif</span> <span class="n">data_type</span> <span class="o">==</span> <span class="s1">&#39;min_value&#39;</span><span class="p">:</span>
                <span class="n">result</span><span class="p">[</span><span class="n">col_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">summary_obj</span><span class="o">.</span><span class="n">min_value</span>
            <span class="k">elif</span> <span class="n">data_type</span> <span class="o">==</span> <span class="s1">&#39;std_variance&#39;</span><span class="p">:</span>
                <span class="n">result</span><span class="p">[</span><span class="n">col_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">summary_obj</span><span class="o">.</span><span class="n">std_variance</span>

        <span class="k">return</span> <span class="n">result</span></div>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2019, FATE_TEAM

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>